layui.use('layer', function() {
	var layer = layui.layer;
	//套餐+-

	$(document).on("click", ".add", function() {
		var a = $(".buy_num").val();
		if(a >= 0) {
			a++;
			$(".buy_num").val(a);
			Calculation();
		} else {
			$(".buy_num").val(0);
			Calculation();
		}

	});

	$(document).on("click", ".reduce", function() {
		var buy_num = $(".buy_num").val();
		if(buy_num > 1) {
			buy_num--;
			$(".buy_num").val(buy_num);
			Calculation();
		} else {
			$(".buy_num").val(1);
			Calculation();
		}

	})

	//套餐交互
	$(".package_list").click(function() {
		$(".floating_box").removeClass("active");
		$(this).addClass("active").siblings().removeClass("active");
		$(this).find("span").addClass("active").parent().siblings().find("span").removeClass("active");
		Calculation()
	})
	//用户名验证
	$(".username").blur(function() {
		var username = $(".username").val();
		var patrn1 = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){1,20}$/;
		if(!patrn1.test(username)) {
			layer.msg("用户名(数字，字母2-20位字符)");
			return false;
		}
	})
	//支付方式交互
	$(".method a").click(function() {
		$(this).addClass("meth_active").siblings().removeClass("meth_active");
		$(this).find("span").addClass("actives").parents().siblings().find("span").removeClass("actives");
	})
	$(".weixin_inner span").click(function() {
		$(".wx-chat").fadeOut();
		// clearInterval(interval);
	});
	$(".buy_num").blur(function() {
		var buy_num = $(".buy_num").val();
		if(buy_num < 1) {
			$(".buy_num").val(1)
		}
		Calculation()
	})

	function Calculation() {
		var data_price = $('.on>.active').attr('data-price');
		var producet_id = $(".on>.active").attr('data-id');
        var data_number = 1;
		var buy_num = $(".buy_num").val();
		mouth_all = data_price * buy_num;
		all_price = parseInt(data_price);

		if(producet_id == '1' || producet_id == '6') {
			// alert(buy_num);
            data_number = buy_num;
			$(".price").addClass("slideInRight animated").html(mouth_all);
		} else {
			$(".price").addClass("slideInRight animated").html(all_price);
            data_number=$('.on>.active').attr('data-number');
		}

	}
    $(".disabl_go_buy").click(function () {
        layer.msg("请登录");
        return false;

    })
	//购买
	$(".go_buy").click(function() {
		var data_price = $('.on>.active').attr('data-price');
		var price=$(".price").text();
		var producet_id = $(".on>.active").attr('data-id'); //产品id
		var buy_num = $(".buy_num").val();
		var username = $(".username").val();
		var patrn1 = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){1,20}$/;
		if(!patrn1.test(username)) {
			layer.msg("用户名(数字，字母2-20位字符)");
			return false;
		}
        var data_number = 1;

        if(producet_id == '1' || producet_id == '6') {
            data_number = buy_num;
        } else {
            data_number=$('.on>.active').attr('data-number');
        }

		var token  = $("#token").val();

		var meth_active = $(".meth_active").attr('data-id');
		if(meth_active == 'wx-pay') {
            src = '/createOrder?month='+data_number+'&pay_type=wxpay&code='+'&product='+producet_id;
            $(".wxcode").attr('src',src);
			$(".wx-chat").show();


            var timesRun = 0;
            var interval = setInterval(function(){
                timesRun += 1;
                if(timesRun === 60){
                    clearInterval(interval);
                }

                $.ajax({
                    url:'/checkorderstatus',
                    type:'post',
                    data:{
                        product: producet_id,
                        pay_type: 'pay',
                        _token:token
                    },
                    timeout:5000,
                    dataType:'json',
                    success:function(data,textStatus,jqXHR){
                        if(data.code == 0){
                            clearInterval(interval);
                            window.location.href = '/user';
                        }
                    },
                })
            }, 8000);

            $(".weixin_inner span").click(function () {
                $(".wx-chat").fadeOut();
                clearInterval(interval);
            })

		} else {

            window.open('/createOrder?month='+data_number+'&pay_type=alipay&code='+'&product='+producet_id, '_blank');

		}
	});



	$(".tab_list li").click(function() {
		var index = $(this).index();
        var buy_num = $(".buy_num").val();
        $(".buy_num").val(1);
		$(this).addClass("on_active").siblings().removeClass("on_active");
		$(".cp_box>.product-fee").eq(index).show().addClass("on bounceIn animated").siblings().hide().removeClass("on bounceIn animated");
		Calculation();
	})
	//高级套餐交互
	$(".hight .package_list").mouseenter(function() {
		var a = $(this).index(".package_list");
		$(".height-float>.floating_box").eq(a).addClass("active");
	});
	$(".hight .package_list").mouseleave(function() {
		$(".height-float>.floating_box").removeClass("active");
	});

	//低级套餐交互
	$(".small .package_list").mouseenter(function() {
		var b = $(this).index(".small .package_list");
		$(".small-float>.floating_box").eq(b).addClass("active");
	});
	$(".small .package_list").mouseleave(function() {
		$(".small-float>.floating_box").removeClass("active");
	});

});